package com.wf.netty.base;

import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

import java.nio.charset.StandardCharsets;

/**
 * 功能描述
 *
 * @author wf
 * @since 2022/5/25
 */
public class MyClientOutHandler extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        System.out.println("step in");
        CompositeByteBuf compositeBuffer = Unpooled.compositeBuffer();
        String header = "Authorization";
        String body = "123";
        compositeBuffer.addComponents(Unpooled.wrappedBuffer(header.getBytes(StandardCharsets.UTF_8)),
                Unpooled.wrappedBuffer(body.getBytes(StandardCharsets.UTF_8)));
        byte[] bytes = header.getBytes(StandardCharsets.UTF_8);

        String s = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(s);
        ctx.writeAndFlush(compositeBuffer);
        super.write(ctx, msg, promise);
    }
}
